//
//  DPChatMessageSetViewController.swift
//  DoctorEPlus
//
//  Created by 王林波 on 2020/12/18.
//  消息设置

import UIKit

class BHChatMessageSetViewController: BHBaseViewController {
    
    lazy var tableView: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.frame = CGRect(x: 0, y: UIScreen.begainY, width: UIScreen.width, height: UIScreen.height-UIScreen.begainY)
        view.register(UINib.init(nibName: "BHMeSettingInfoTableViewCell", bundle: nil), forCellReuseIdentifier: "BHMeSettingInfoTableViewCell")
        view.register(UINib.init(nibName: "BHConversationSettingSwitchCell", bundle: nil), forCellReuseIdentifier: "BHConversationSettingSwitchCell")
        
        view.delegate = self
        view.dataSource = self
        return view
    }()
    

   @objc  var conversation: TUIConversationCellData!
    
    fileprivate var settingNameArray: [String] = ["置顶该消息","拉黑","举报"]
    fileprivate var inBlack: Bool = false // 是否是黑名单
    override func viewDidLoad() {
        super.viewDidLoad()

        self.matchBlack()
        if self.conversation.groupID.count > 0  {
            self.settingNameArray = ["置顶该消息","屏蔽群消息","举报"]
        }
        self.bh_navigationBar.title_label.text = "设置"
        self.view.addSubview(self.tableView)
        
    }
}
extension BHChatMessageSetViewController: UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.settingNameArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
   
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row != 2 {
            return self.cellForSwitch(indexPath)
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "BHMeSettingInfoTableViewCell", for: indexPath) as! BHMeSettingInfoTableViewCell
            cell.name_label.text = "举报"
            cell.des_label.text = ""
            return cell
        }
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 2 {
            BHRouter.shared.bh_report(type: 3, businessId: self.conversation.userID)
        }
    }
}

extension BHChatMessageSetViewController {
    func cellForSwitch(_ indexPath: IndexPath) -> BHConversationSettingSwitchCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BHConversationSettingSwitchCell", for: indexPath) as! BHConversationSettingSwitchCell
        cell.name_label.text = self.settingNameArray[indexPath.row]
        if indexPath.row == 0 {
            cell.switchBtn.isOn = self.conversation.isOnTop
        }else {
            cell.switchBtn.isOn = self.inBlack
        }
        
        cell.switchChangeValue = {[weak self] (switchOpen) in
            if indexPath.row == 0 { // 置顶
                self?.onTopConversation()
            }else { // 拉黑
                if self?.inBlack == true {
                    self?.removeFromBlack()
                }else {
                    self?.addToBlackList()
                }
            }
        }
        return cell
    }
}

extension BHChatMessageSetViewController {
    func matchBlack()  {
        V2TIMManager.sharedInstance()?.getBlackList({ (result) in
            if result != nil {
                for info in result! {
                    if info.userID == self.conversation.userID {
                        self.inBlack = true
                        break
                    }
                }
            }
            self.tableView.reloadData()
            
        }, fail: { (code, errorMsg) in
            if errorMsg?.isEmpty == false {
                BHToast.toast(text: errorMsg)
            }
        })
    }
    
    func addToBlackList()  {
        SVProgressHUD.show()
        V2TIMManager.sharedInstance()?.add(toBlackList: [self.conversation.userID], succ: { (resultArray) in
            SVProgressHUD.dismiss()
            if resultArray == nil {
                BHToast.toast(text: "操作失败")
                return
            }
            self.inBlack = true
            
            self.tableView.reloadData()
        }, fail: { (code, errorMsg) in
            SVProgressHUD.dismiss()

            if errorMsg?.isEmpty == false {
                BHToast.toast(text: errorMsg)
            }
        })
    }
    
    func removeFromBlack()  {
        SVProgressHUD.show()
        V2TIMManager.sharedInstance()?.delete(fromBlackList: [self.conversation.userID], succ: { (resultArray) in
            SVProgressHUD.dismiss()
            if resultArray == nil {
                BHToast.toast(text: "操作失败")
                return
            }
            self.inBlack = false
            
            self.tableView.reloadData()
        }, fail: { (code, errorMsg) in
            SVProgressHUD.dismiss()

            if errorMsg?.isEmpty == false {
                BHToast.toast(text: errorMsg)
            }
        })
    }
    
    
    func onTopConversation()  {
        SVProgressHUD.show()

        let upCell: Bool = !self.conversation.isOnTop
        V2TIMManager.sharedInstance()?.pinConversation(self.conversation.conversationID, isPinned: upCell, succ: {
            SVProgressHUD.dismiss()
            self.conversation.isOnTop = !self.conversation.isOnTop
            self.tableView.reloadData()
            
        }, fail: { (code, errorMsg) in
            SVProgressHUD.dismiss()

            if errorMsg?.isEmpty == false {
                BHToast.toast(text: errorMsg)
            }
        })
    }
}
